// Main Javascript file
// 
// Created by Christopher Biagini on 2007-07-08.
//  Copyright (c) 2007. All rights reserved.

// this could be a two-way proxy eventually, but for now it'll just
// hold whatever object we get from the server
var iTunes;

// Setup click handlers and such
function init() {
  document.getElementById("refresh").onclick    = function() {send_command("info"); return false;};
  document.getElementById("play").onclick  = function() {send_command("playpause"); return false;};
  document.getElementById("pause").onclick  = function() {send_command("playpause"); return false;};
  document.getElementById("back_track").onclick = function() {send_command("back_track"); return false;};
  document.getElementById("next_track").onclick = function() {send_command("next_track"); return false;};
  document.getElementById("vol_up").onclick     = function() {send_command("vol_up"); return false;};
  document.getElementById("vol_down").onclick   = function() {send_command("vol_down"); return false;};
  document.getElementById("rat_up").onclick     = function() {send_command("rat_up"); return false;};
  document.getElementById("rat_down").onclick   = function() {send_command("rat_down"); return false;};
    
  send_command("info");
  setInterval("send_command(\"info\");", 10000); // update display every 10 seconds
}

// Send request to server, use response to update display
function send_command(command) {      
  var req = new XMLHttpRequest();
  req.open("GET", "/itunes?action=" + command, true); // GET or POST more appropriate here?
	req.send("");
	
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            iTunes = eval('(' + req.responseText + ')');
            update_display();
        } else {
            error("There was a problem retrieving data from the server.");
        }
    }
  };  
}

function getRatingStars(numStars) {
  switch (numStars) {
    case 0:
      return "•••••";
      break;
    case 1:
      return "★••••";
      break;
    case 2:
      return "★★•••";
      break;
    case 3:
      return "★★★••";
      break;
    case 4:
      return "★★★★•";
      break;
    case 5:
      return "★★★★★";
      break;
    default:
      return "--";
  }
}

// Wrapper method to update song display
function update_display() {
  document.getElementById("album").innerText  = iTunes.current_track.album;
  document.getElementById("artist").innerText = iTunes.current_track.artist;
  document.getElementById("rating").innerText = getRatingStars(iTunes.current_track.rating);
  document.getElementById("name").innerText   = iTunes.current_track.name;
  document.getElementById("volume").innerText = iTunes.sound_volume;

  if (iTunes.player_state == "paused" || iTunes.player_state == "stopped") {
    document.getElementById("play").style.display = "inline";
    document.getElementById("pause").style.display = "none";
  } else {
    document.getElementById("play").style.display = "none";
    document.getElementById("pause").style.display = "inline";
  }
}

// Handle errors returned from server
function error(errorMsg) {
  document.getElementById("album").innerText  = "--";
  document.getElementById("artist").innerText = "--";
  document.getElementById("rating").innerText = "--";
  document.getElementById("name").innerText   = "--";
  document.getElementById("volume").innerText = "--";

  document.getElementById("play").style.display = "inline";
  document.getElementById("pause").style.display = "none";
}

// Kick things off on load
window.onload = init;
